package lyl.util;

import java.util.*;
import java.io.*;

/**
 * <p>
 * 在本地磁盘的jar文件中寻找自己所需要的class
 * </p>
 * <p>
 * 用法，只需要提供本地磁盘路径和所需要的class的全称（包括package等信息）
 * </p>
 * <p>
 * 例如 org.w3c.dom.Document
 * </p>
 */
public class FindClass {

	public static int count = 0;

	public FindClass() {
	}

	private static void FindClassInLocalSystem(String path, String classname) {
		if (path.charAt(path.length() - 1) != '\\')
			path += '\\';
		File file = new File(path);
		if (!file.exists()) {
			System.out
					.println("--------------------------------------------------------");
			System.out
					.println("error:   Path   not   Existed!   Please   Check   it   out!");
			System.out
					.println("--------------------------------------------------------");
			return;
		}
		String[] filelist = file.list();
		for (int i = 0; i < filelist.length; i++) {
			File temp = new File(path + filelist[i]);
			if ((temp.isDirectory() && !temp.isHidden() && temp.exists())) {
				FindClassInLocalSystem(path + filelist[i], classname);
			} else {
				if (filelist[i].endsWith("jar")) {
					try {
						java.util.jar.JarFile jarfile = new java.util.jar.JarFile(
								path + filelist[i]);
						for (Enumeration e = jarfile.entries(); e
								.hasMoreElements();) {
							String name = e.nextElement().toString();
							if (name.equals(classname)) {
								System.out
										.println("--------------------------------------------------------");
								System.out.println("No." + ++FindClass.count);
								System.out.println("jar   package:" + path
										+ filelist[i]);
								System.out.println(name);
								System.out
										.println("--------------------------------------------------------");
							}
						}
					} catch (Exception eee) {
					}
				}
			}
		}

	}

	static public void main(String[] args) {
		if (args.length < 2) {
			System.out.println("Usage:");
			System.out
					.println("the   first   parameter:[path   to   find]   eg.     C:\\     d:\\Jbuilder       Tip:only   '\\'   can   be   used   here");
			System.out
					.println("the   first   parameter:[class   to   find]   eg.   org.w3c.dom.Document");
			return;
		}
		String absoluteclassname = args[1].replace('.', '/') + ".class";

		System.out.println("Find   class   [" + args[1] + "]   in   path   ["
				+ args[0] + "]   results:");
		FindClassInLocalSystem(args[0], absoluteclassname);
		if (FindClass.count == 0) {
			System.out
					.println("--------------------------------------------------------");
			System.out.println("Sorry,No   such   Jar!");
			System.out
					.println("--------------------------------------------------------");
		}
		System.out.println("Find   Process   Ended!   Total   Results:"
				+ FindClass.count);
	}
}